using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// 承包商_考试记录
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class CBS_ExamRecordController : CoreApiController
    {
        private readonly CBS_ExamRecordService _service;
        /// <summary>
        /// CBS_ExamRecord
        ///</summary>
        public CBS_ExamRecordController(CBS_ExamRecordService service)
        {
            _service = service;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(CBS_ExamRecordController) + "_" + nameof(CBS_ExamRecordController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<CBS_ExamRecordDto> pagingQueryModel)
        {
            IPageModel<CBS_ExamRecordDto> result;
            result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();

            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Id,
                a.CBS_TrainRecord?.Mark01,//课件名称a.CBS_Course?.TrainingCoursewareName,
                a.ExaminationDate,//考试日期
                a.CBS_List?.ContractorName,//承包商名称
                a.CBS_User?.PersonnelName,//人员名称
                a.IsQualified,//IsPass,//是否合格
                a.UpdateTime,//更新日期
                a.UpdateUserText//更新人
            }
            ).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(CBS_ExamRecordController) + "_" + nameof(CBS_ExamRecordController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(CBS_ExamRecordDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto,true,noUpdateProperties:new System.Collections.Generic.List<string>() {
                     nameof(CBS_ExamRecordDto.CreateFactory),
                     nameof(CBS_ExamRecordDto.ExaminationType),
                     nameof(CBS_ExamRecordDto.CreateDept),
                     nameof(CBS_ExamRecordDto.UpdateDept),
                });
                return this.JsonSuccess(string.Empty);
            }
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var retObj = new
            {
                model.Id,
                model.TrainRecordId,//CBSCourseId,//培训课件ID
                model.Examiner,//考核人
                model.ExaminationDate,//考试日期
                model.DegreeOfDifficulty,//难易程度
                model.Fraction,//分数
                model.CBSListId,//承包商ID
                model.IsQualified,//IsPass,//是否合格
                model.AttachmentId,//Annex,//附件
                model.Remarks,//备注
            };
            return this.JsonSuccess(retObj);
        }



        #endregion


    }
}
